package com.pricl.frame.common.utils;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtils {
	
	public static final Logger logger = LoggerFactory.getLogger(JsonUtils.class); 

	/**
	 * java对象转换成json对象字符串
	 * 
	 * @param obj
	 * @return String
	 */
	public static String toString(Object obj) {
		if (obj == null) {
			return "";
		}
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		mapper.setSerializationInclusion(Include.NON_NULL);
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.error("解析Json出错",e);
			return "";
		}
	}

	/**
	 * java对象转换成json对象字节数组
	 * 
	 * @param obj
	 * @return String
	 */
	public static byte[] toBytes(Object obj) {
		if (obj == null) {
			return null;
		}
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		mapper.setSerializationInclusion(Include.NON_NULL);
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			return mapper.writeValueAsBytes(obj);
		} catch (Exception e) {
			logger.error("解析Json出错",e);
			return null;
		}
	}

	/**
	 * <p>
	 * 解析为对象
	 * </p>
	 * 
	 * @param in
	 * @param clazz
	 * @return
	 */
	public static <T> T toObject(java.io.InputStream in, Class<T> clazz) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return mapper.readValue(in, clazz);
		} catch (Exception e) {
			logger.error("解析Json出错",e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> toList(java.io.InputStream in, Class<T> clazz) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
		try {
			return (List<T>) mapper.readValue(in, javaType);
		} catch (Exception e) {
			logger.error("解析Json出错",e);
			return null;
		}
	}

	/**
	 * <p>
	 * 解析为对象
	 * </p>
	 * 
	 * @param in
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T toObject(byte[] in, Class<T> clazz) {
		if (clazz.isAssignableFrom(Date.class)) {
			String val = new String(in).replaceAll("\"", "");
			String format = "yyyy-MM-dd HH:mm:ss";
			try {
				return (T) new SimpleDateFormat(format.substring(0, val.length())).parse(val);
			} catch (ParseException e) {
			}
		}
		ObjectMapper mapper = new ObjectMapper();
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return mapper.readValue(in, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("解析Json出错",e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> toList(byte[] in, Class<T> clazz) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
		try {
			return (List<T>) mapper.readValue(in, javaType);
		} catch (Exception e) {
			logger.error("解析Json出错",e);
			return null;
		}
	}

	/**
	 * <p>
	 * 解析为对象
	 * </p>
	 * 
	 * @param str
	 * @param clazz
	 * @return
	 */
	public static <T> T toObject(String str, Class<T> clazz) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return mapper.readValue(str, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("解析Json出错",e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> toList(String str, Class<T> clazz) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
		try {
			return (List<T>) mapper.readValue(str, javaType);
		} catch (Exception e) {
			logger.error("解析Json出错",e);
			return null;
		}
	}
}
